package com.sychip.fhc.app.data.repo.impl


import com.sychip.fhc.app.data.repo.WsnRssiHisRepo
import com.sychip.fhc.app.data.source.convert.toDto
import com.sychip.fhc.app.data.source.convert.toEntity
import com.sychip.fhc.app.data.source.dao.WsnRssiHisDao
import com.sychip.fhc.app.data.source.dto.WsnRssiHisDto
import com.sychip.fhc.di.IoDispatcher
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton


@Singleton
class WsnRssiHisRepoImpl @Inject constructor(
    private val wsnRssiHisDao: WsnRssiHisDao,
    @IoDispatcher private val dispatcher: CoroutineDispatcher
) : WsnRssiHisRepo {

    override suspend fun getAll(usbId: String): List<WsnRssiHisDto> {
        return withContext(dispatcher) {
            wsnRssiHisDao.getAll(usbId).toDto()
        }
    }

    override suspend fun getAllParseIncludes(usbId: String, rssiType: Int, rssiParse: List<String>): List<String> {
        return withContext(dispatcher) {
            wsnRssiHisDao.getAllParseLikeWithParts(usbId, rssiType, rssiParse)
        }
    }

    override suspend fun add(usbId: String, fullRssi: String, rssiType: Int, rssiParse: String,dateTime: Long): WsnRssiHisDto {
        val task = WsnRssiHisDto(
            usbId = usbId,
            fullRssi = fullRssi,
            rssiType = rssiType,
            rssiParse = rssiParse,
            dateTime = dateTime,
        )
        wsnRssiHisDao.upsert(task.toEntity())
        return task
    }

    override suspend fun deleteByFromTime(dateTime: Long): Int {
        return wsnRssiHisDao.deleteByFromTime(dateTime)
    }

    override suspend fun deleteAll(usbId: String): Int {
        return wsnRssiHisDao.deleteAll(usbId)
    }


}
